<?php
/**
 * Created by PhpStorm.
 * User: zlkmu
 * Date: 2017/8/12
 * Time: 22:58
 */
    require_once ('../Common.php');
    require_once ('../BusinessApprove/BusinessApp.php');
    require_once ('../SQLBase.php');
    require_once ('../LoanFee/LoanFee.php');

    class BusinessFee{

        //作废报销单过程函数
        //获取关联信息
        //作废嫌疑人报销单
        //作废超标报销单
        //作废公务卡报销单
        //作废基本报销单
        function disposeBusinessFeeApprove($feeID){
            if($feeID == ''){
                return;
            }
            $conn = new SQLBase();

            $sql = "SELECT suspect_fee_id, over_standard_id, un_official_app_id FROM travel_fee_approve WHERE travel_fee_id = '$feeID'";
            $r = $conn->queryData($sql);
            if(!$r){
                exit(json_encode(array('state'=>0, 'error'=>'DB_ERROR')));
            }
            $data = $r->fetch_array();

            $conn->queryData('SET AUTOCOMMIT = 0');
            $conn->queryData('BEGIN');

            $sql = "UPDATE travel_fee_approve SET is_dispose = 1 WHERE travel_fee_id = '$feeID'";
            $r = $conn->queryData($sql);

            $sql = "UPDATE suspect_money SET is_dispose = 1 WHERE suspect_ID = '".$data['suspect_fee_id']."'";
            $r = $conn->queryData($sql);

            $sql = "UPDATE over_standard SET is_dispose = 1 WHERE over_standard_id = '".$data['over_standard_id']."'";
            $r = $conn->queryData($sql);

            $sql = "UPDATE unuse_official_approve SET is_dispose = 1 WHERE unuse_official_id = '".$data['un_official_app_id']."'";
            $r = $conn->queryData($sql);

            if(!$r){
                $conn->queryData('ROLLBACK');
                $conn->queryData("SET AUTOCOMMIT = 1");
                exit(json_encode(array('state'=>0, 'error'=>'DB_ERROR')));
            }
            else{
                $conn->queryData('COMMIT');
                $conn->queryData("SET AUTOCOMMIT = 1");
            }
        }


        //创建报销单总过程函数
        //0 作废之前所有相关单据
        //1 更改差旅申请单状态为待报销
        //2 创建嫌疑人报销单
        //3 创建超标报销单
        //4 创建位使用公务卡报销单
        //5 创建基本报销单
        //6 创建详细数据
        function createBusinessApproveFee($feeData){
            $this->disposeBusinessFeeApprove($feeData['history_fee_ID']);

            $b_app = new BusinessApp();
            $r = $b_app->changeBusinessAppState($feeData['approval_ID'], 3);
            if(!$r){
                $returnArray['state'] = 0;
                $returnArray['state_error'] = 'FAILED_TO_UPDATE_APPROVAL';
                return $returnArray;
            }

            $returnArray = array('suspect_fee_id'=>'', 'overStandard_fee_id'=>'', 'un_official_app_id'=>'',
                'official_fee_id'=>'', 'approval_id'=>'');
            //创建嫌疑人费用报销申请
            if(isset($feeData['suspect_fee_data'])){
                $r = $this->createSuspectFeeApprove($feeData['suspect_fee_data'], $feeData['approval_ID'], $feeData['approve_userID'], $feeData['approve_sectionID']);
                if(!$r){
                    $returnArray['state'] = 0;
                    $returnArray['state_error'] = 'FAILED_TO_CREATE_SUSPECT_FEE';
                    return $returnArray;
                }
                else{
                    $returnArray['suspect_fee_id'] = $r;
                }
            }
            //创建超标交通工具报销申请
            if(isset($feeData['overStandard'])){
                $r = $this->createOverStandardApprove($feeData['overStandard'], $feeData['approval_ID'], $feeData['approve_userID'], $feeData['approve_sectionID']);
                if(!$r){
                    $returnArray['state'] = 0;
                    $returnArray['state_error'] = 'FAILED_TO_CREATE_OVERSTANDARD';
                    return $returnArray;
                }
                else{
                    $returnArray['overStandard_fee_id'] = $r;
                }
            }

            //创建非公务卡报销强制费用审批单数据
            if($feeData['official_fee_info']['is_need_unofficial'] == 1){
                $r = $this->createUnUseOfficialCardApprove($feeData['official_fee_info']['un_official_reason'],
                    $feeData['approve_userID'], $feeData['approve_sectionID']);
                if(!$r){
                    $returnArray['state'] = 0;
                    $returnArray['state_error'] = 'FAILED_TO_CREATE_UN_OFFICIAL';
                    return $returnArray;
                }
                else{
                    $returnArray['un_official_app_id'] = $r;
                }
            }


            //创建公务卡报销信息条目
            $r = $this->createOfficialFeeItem($feeData['official_fee_info']);
            if(!$r){
                $returnArray['state'] = 0;
                $returnArray['state_error'] = 'FAILED_TO_CREATE_OVERSTANDARD';
                return $returnArray;
            }
            else{
                $returnArray['official_fee_id'] = $r;
            }

            //创建实际报销单
            $r = $this->createNewBusinessApproveFee($feeData, $returnArray);
            if(!$r){
                $returnArray['state'] = 0;
                $returnArray['state_error'] = 'FAILED_TO_CREATE_FEE_APPROVE';
                return $returnArray;
            }
            $returnArray['approval_id'] = $r;

            //创建实际报销单条目
            $r = $this->createBusinessApproveFeeItem($feeData, $r);
            if($r['state'] == 0){
                $returnArray['state'] = 0;
                $returnArray['state_error'] = 'FAILED_TO_CREATE_FEE_ITEM';
            }

            $returnArray['state'] = 1;

            return $returnArray;
        }

        //辅助函数，创建报销单实际条目
        function createBusinessApproveFeeItem($feeData, $feeID){
            $conn = new SQLBase();
            //开始插入数据
            $conn->queryData('SET AUTOCOMMIT=0');
            $conn->queryData("BEGIN");
            $error = false;
            for ($i = 0; $i < count($feeData['data']); $i++) {
                $query = "INSERT travel_fee_detail(travel_fee_id, start_dest, start_county, start_time, end_dest, 
                        end_county, end_time, stay_days, road_days, car_fee, boat_fee, flight_fee, hotel_fee, food_fee, 
                        inci_fee, other_fee, total_fee, official_fee, suspect_fee, reward_fee, car_main_fee,
                        oil_fee, pass_fee, distance) VALUES
                        ('" . $feeID . "'";
                $query .= ",'" . $feeData['data'][$i]['start_addr'] . "','" . $feeData['data'][$i]['start_county'] . "','"
                    . $feeData['data'][$i]['start_time'] . "','" . $feeData['data'][$i]['end_addr'] . "','"
                    . $feeData['data'][$i]['end_county'] . "','" . $feeData['data'][$i]['end_time'] . "','"
                    . $feeData['data'][$i]['stay_days'] . "','" . $feeData['data'][$i]['road_days'] . "','"
                    . $feeData['data'][$i]['car_fee'] . "','" . $feeData['data'][$i]['boat_fee'] . "','"
                    . $feeData['data'][$i]['flight_fee'] . "','" . $feeData['data'][$i]['hotel_fee'] . "','"
                    . $feeData['data'][$i]['food_fee'] . "','" . $feeData['data'][$i]['inci_fee'] . "','"
                    . $feeData['data'][$i]['other_fee'] . "','" . $feeData['data'][$i]['total_fee'] . "','"
                    . $feeData['data'][$i]['official_fee'] . "','" . $feeData['data'][$i]['suspect_fee'] . "','"
                    . $feeData['data'][$i]['reward_fee'] ."','" . $feeData['data'][$i]['car_main_fee'] . "','"
                    . $feeData['data'][$i]['oil_fee'] ."','" . $feeData['data'][$i]['pass_fee'] . "','"
                    . $feeData['data'][$i]['distance']."')";
                $r = $conn->queryData($query);
                if ($r) {
                    $error = false;
                } else {
                    $error = true;
                }
            }
            if (!$error) {
                $conn->queryData('COMMIT');
                $returnArray['state'] = 1;
                $returnArray['state_error'] = '';
            } else {
                $conn->queryData('ROLLBACK');
                $returnArray['state'] = 0;
                $returnArray['state_error'] = "FAILED_TO_INSERT_FEEITEM";
            }
            $conn->queryData("SET AUTOCOMMIT=1");
            return $returnArray;
        }

        //辅助函数，创建实际报销单
        function createNewBusinessApproveFee($feeData, $returnArray)
        {
            //计算总金额
            $total_fee = 0;
            $total_suspect_fee = 0;  //嫌疑人费用
            $total_reward_fee = 0;  //网上追逃奖励金
            for ($i = 0; $i < count($feeData['data']); $i++){
                $total_fee += $feeData['data'][$i]['total_fee'];
                $total_reward_fee += $feeData['data'][$i]['reward_fee'];
                $total_suspect_fee += $feeData['data'][$i]['suspect_fee'];
            }

            $conn = new SQLBase();
            $id = uniqid();

            $query = "INSERT travel_fee_approve(travel_fee_id, write_ID, section_ID, travel_approve_id, 
                    pre_total_fee, suspect_fee, reward_fee, suspect_fee_id, 
                    over_standard_id, official_fee_id, un_official_app_id, write_time) VALUES('" .
                      $id. "','" . $feeData['approve_userID'] . "','" . $feeData['approve_sectionID'] . "','" .
                      $feeData['approval_ID'] . "','" . $total_fee . "','" .
                      $total_suspect_fee . "','" . $total_reward_fee . "','" . $returnArray['suspect_fee_id'] . "','" .
                      $returnArray['overStandard_fee_id'] . "','" . $returnArray['official_fee_id'] . "','" . $returnArray['un_official_app_id']."',NOW())";

            $result = $conn->queryData($query);
            if ($result)
                return $id;
            else
                return false;
        }


        //辅助函数，设置嫌疑人审批信息
        function createSuspectFeeApprove($suspect_fee_data, $appID, $userID, $sectionID)
        {
            $conn = new SQLBase();

            $id = uniqid();
            $query = "INSERT suspect_money(suspect_ID, write_ID, section_ID, event_ID, suspect_name, suspect_travel_money, suspect_life_money, suspect_other_money, 
              has_receipe, no_receipe_reason, write_time) VALUES('$id','$userID','$sectionID','$appID','" . $suspect_fee_data['suspect_name'] . "','" . $suspect_fee_data['suspect_road_fee'] . "','"
                . $suspect_fee_data['suspect_life_fee'] . "','" . $suspect_fee_data['suspect_other_fee'] . "','" . $suspect_fee_data['has_receipe'] . "','" . $suspect_fee_data['no_receipe_reason'] . "',NOW())";
            $r = $conn->queryData($query);
            if ($r) {
                return $id;
            } else {
                return false;
            }
        }

        //辅助函数，设置超标审批信息
        function createOverStandardApprove($over_fee_data, $appID, $userID, $sectionID)
        {

            $conn = new SQLBase();

            $id = uniqid();
            $query = "INSERT over_standard(over_standard_id, write_ID, section_ID, event_ID, over_reason, write_time) VALUES('$id', '$userID', '$sectionID', '$appID','" .
                $over_fee_data['over_reason'] . "',NOW())";

            $r = $conn->queryData($query);
            if ($r) {
                return $id;
            } else {
                return false;
            }
        }

        //辅助函数，创建公务卡报销信息条目
        function createOfficialFeeItem($official_fee_data){
            $conn = new SQLBase();
            $id = uniqid();
            $sql = "INSERT INTO official_fee_item VALUES('$id','".$official_fee_data['official_hotel_fee']."','".
                        $official_fee_data['official_plane_fee']."','".$official_fee_data['official_oil_fee']."','".
                        $official_fee_data['official_car_maintain_fee']."','".$official_fee_data['official_total_fee']."','".
                        $official_fee_data['max_official_hotel_fee']."','".$official_fee_data['max_official_plane_fee']."','".
                        $official_fee_data['max_official_oil_fee']."','".$official_fee_data['max_official_car_maintain_fee']."','".
                        $official_fee_data['max_official_fee']."')";
            $r = $conn->queryData($sql);
            if($r){
                return $id;
            }
            else{
                return false;
            }
        }

        //辅助函数，创建未使用公务卡报销强制项目报销单
        function createUnUseOfficialCardApprove($reason, $userID, $sectionID){
            $conn = new SQLBase();

            $id = uniqid();
            $query = "INSERT unuse_official_approve(unuse_official_id, reason, write_ID, section_ID, write_time) 
            VALUES('$id', '$reason', '$userID', '$sectionID', NOW())";
            $r = $conn->queryData($query);
            if ($r) {
                return $id;
            } else {
                return false;
            }
        }

        //辅助函数，获取用户所有报销单
        function getBusinessFeeByUser($user_id,  $state){
            $conn = new SQLBase();
            $sql = "SELECT travel_fee_approve.*, user.user_name, allsection.section_name, official_fee_item.official_fee FROM travel_fee_approve, official_fee_item, user, allsection WHERE
                     travel_fee_approve.write_ID = '$user_id' AND travel_fee_approve.section_ID = allsection.section_ID AND travel_fee_approve.official_fee_id = official_fee_item.official_fee_id 
                     AND allsection.section_state = 1 AND travel_fee_approve.write_ID = user.user_ID AND travel_fee_approve.is_dispose = 0 ";
            //如果不是全部状态，则为0待审批，1已同意 2已驳回,3已报销
            if($state != 4){
                $sql .= " AND travel_fee_approve.state='$state' ORDER BY write_time DESC";
            }//否则查询全部
            else{
                $sql .= " ORDER BY write_time DESC";
            }
            $r = $conn->queryData($sql);

            $result_array = array();
            if($r){
                $result_array['state'] = 1;
                $result_array['data'] = array();
                $result_array['type'] = 1;
                for($i = 0; $i < $r->num_rows; $i++){
                    $row = $r->fetch_array();
                    $result_array['data'][$i] = array(
                        'fee_id'=>$row['travel_fee_id'],
                        'user_name'=>$row['user_name'],
                        'section_name'=>$row['section_name'],
                        'travel_approve_id'=>$row['travel_approve_id'],
                        'pre_total_fee'=>$row['pre_total_fee'],
                        'pre_official_fee'=>$row['official_fee'],
                        'state'=>$row['state'],
                        'state_detail'=>$row['state_detail'],
                        'suspect_fee_id'=>$row['suspect_fee_id'],
                        'over_standard_id'=>$row['over_standard_id'],
                        'un_official_app_id'=>$row['un_official_app_id']);
                }
                $result_array['totalCount'] = $r->num_rows;
            }
            else{
                $result_array['state'] = 0;
                $result_array['error'] = 'DB_ERROR';
            }
            return $result_array;

        }

        //辅助函数，获取报销单详细信息
        //1 获取报销基本信息，fee_data
        //2 获取报销详细信息，fee_data_item
        //3 获取对应审批信息，fee_app_data
        //4 获取与之对应的最后一条被拒绝的信息ID, fee_history_item
        //5 获取财务核销基本信息和详细信息
        function getBusinessFeeDetail($feeID){
            $result_array = array();
            $r = $this->getBasiceTravelFee($feeID);
            if(!$r){
                exit(json_encode(array('state'=>0, 'error'=>'DB_ERROR')));
            }

            $result_array['fee_data'] = $r;

            $r = $this->getDetailTravelFeeItem($result_array['fee_data']['travel_fee_id']);
            if(!$r){
                exit(json_encode(array('state'=>0, 'error'=>'DB_ERROR')));
            }

            $result_array['fee_data_item'] = $r;

            $b_app = new BusinessApp();
            $r = $b_app->getBusinessApproveDetail($result_array['fee_data']['travel_approve_id']);
            if($r['state'] == 0){
                exit(json_encode(array('state'=>0, 'error'=>'DB_ERROR')));
            }

            $result_array['travel_app_data'] = $r['data'][0];

            $r = $this->getLastHistoryRefuseFee($result_array['fee_data']['travel_approve_id'], $result_array['fee_data']['travel_fee_id']);
            if($r == -1){
                exit(json_encode(array('state'=>0, 'error'=>'DB_ERROR')));
            }

            $result_array['fee_history_id'] = $r;

            $r = $this->getFinanceCheckedFeeDetail($feeID);
            if($r == -1){
                exit(json_encode(array('state'=>0, 'error'=>'DB_ERROR')));
            }

            $result_array['finance_check_data'] = $r;


            if($result_array['finance_check_data'] != ''){
                $r = $this->getFinanceCheckedFeeItem($result_array['finance_check_data']['finance_approve_fee_id']);
                if(!$r){
                    exit(json_encode(array('state'=>0, 'error'=>'DB_ERROR')));
                }
            }
            else{
                $r = '';
            }


            $result_array['finance_check_item_data'] = $r;

            $result_array['state'] = 1;

            return $result_array;
        }

        //辅助函数，获取财务复核后的基本数据
        function getFinanceCheckedFeeDetail($feeID){
            $conn = new SQLBase();
            $sql = "SELECT finance_approve_business_fee.*,  allsection.section_name, user.user_name FROM finance_approve_business_fee,  allsection, user WHERE approve_fee_id = '$feeID' 
                    AND finance_approve_business_fee.section_id = allsection.section_ID AND user.user_ID = finance_approve_business_fee.write_id";
            $r = $conn->queryData($sql);
            if($r){
                if($r->num_rows > 0){
                    $row = $r->fetch_array();
                    $result_array = array('finance_approve_fee_id'=>$row['finance_approve_fee_id'], 'section_id'=>$row['section_id'],
                        'write_id'=>$row['write_id'], 'total_fee'=>$row['total_fee'], 'official_fee'=>$row['official_fee'],
                        'check_time'=>$row['check_time'], 'section_name'=>$row['section_name'], 'finance_user_name'=>$row['user_name']);
                }
                else{
                    $result_array = '';
                }
                return $result_array;
            }
            else{
                return -1;
            }
        }

        //辅助函数，获取财务复核后的详细报销数据,$feeID=>财务复核数据ID
        function getFinanceCheckedFeeItem($feeID){
            $conn = new SQLBase();
            $sql = "SELECT * FROM finance_approve_business_fee_detail WHERE finance_fee_id = '$feeID'";
            $r = $conn->queryData($sql);
            $result_array = array();
            if($r){
                for($i = 0; $i < $r->num_rows; $i++) {
                    $row = $r->fetch_array();
                    $result_array[$i]['start_dest'] = $row['start_dest'];
                    $result_array[$i]['start_county'] = $row['start_county'];
                    $result_array[$i]['start_time'] = $row['start_time'];
                    $result_array[$i]['end_dest'] = $row['end_dest'];
                    $result_array[$i]['end_county'] = $row['end_county'];
                    $result_array[$i]['end_time'] = $row['end_time'];
                    $result_array[$i]['road_days'] = $row['road_days'];
                    $result_array[$i]['stay_days'] = $row['stay_days'];
                    $result_array[$i]['car_fee'] = $row['car_fee'];
                    $result_array[$i]['boat_fee'] = $row['boat_fee'];
                    $result_array[$i]['flight_fee'] = $row['flight_fee'];
                    $result_array[$i]['hotel_fee'] = $row['hotel_fee'];
                    $result_array[$i]['food_fee'] = $row['food_fee'];
                    $result_array[$i]['inci_fee'] = $row['inci_fee'];
                    $result_array[$i]['other_fee'] = $row['other_fee'];
                    $result_array[$i]['suspect_fee'] = $row['suspect_fee'];
                    $result_array[$i]['reward_fee'] = $row['reward_fee'];
                    $result_array[$i]['car_main_fee'] = $row['car_main_fee'];
                    $result_array[$i]['oil_fee'] = $row['oil_fee'];
                    $result_array[$i]['pass_fee'] = $row['pass_fee'];
                    $result_array[$i]['distance'] = $row['distance'];
                }
                return $result_array;
            }
            else{
                return false;
            }

        }

        //辅助函数，获取关联最后一条被拒绝的报销信息ID
        private function getLastHistoryRefuseFee($appID, $feeID){
            $conn = new SQLBase();
            $query = "SELECT  travel_fee_id FROM travel_fee_approve WHERE travel_approve_id = '$appID' AND state = 2 
                    AND travel_fee_id != '$feeID' AND travel_fee_approve.is_dispose = 1 ORDER BY write_time DESC";
            $r = $conn->queryData($query);

            if($r){
                if($r->num_rows > 0){
                    $row = $r->fetch_array();
                    return $row['travel_fee_id'];
                }
                else{
                    return '';
                }
            }
            else{
                return -1;
            }
        }

        //辅助函数，获取报销基本信息,包含获取基本报销信息，绑定的嫌疑人费用信息，超标费用信息，公务卡信息
        function getBasiceTravelFee($feeID)
        {
            $conn = new SQLBase();
            $query = "SELECT * FROM travel_fee_approve WHERE travel_fee_id='$feeID'";
            $result = $conn->queryData($query);
            $result_array = array();
            if ($result) {
                $data = $result->fetch_array();
                $result_array['travel_fee_id'] = $data['travel_fee_id'];
                $result_array['travel_approve_id'] = $data['travel_approve_id'];
                $result_array['pre_total_fee'] = $data['pre_total_fee'];
                $result_array['pre_official_fee'] = $data['pre_official_fee'];
                $result_array['total_fee'] = $data['total_fee'];
                $result_array['official_fee'] = $data['official_fee'];
                $result_array['reward_fee'] = $data['reward_fee'];
                $result_array['suspect_fee'] = $data['suspect_fee'];
                $result_array['section_manager_ID'] = $data['section_manager_ID'];
                $result_array['section_leader_ID'] = $data['section_leader_ID'];
                $result_array['leader_ID'] = $data['leader_ID'];
                $result_array['write_time'] = $data['write_time'];
                $result_array['state'] = $data['state'];
                $result_array['state_detail'] = $data['state_detail'];
                $result_array['section_approve_opinion'] = $data['section_approve_opinion'];
                $result_array['section_leader_opinion'] = $data['section_leader_opinion'];
                $result_array['leader_opinion'] = $data['leader_opinion'];
                $result_array['self_drive_img'] = $data['self_drive_img'];


                $result_array['is_suspect_fee'] = $data['suspect_fee_id'] == '' ? 0 : 1;
                $result_array['suspect_fee_data'] = $this->getSuspectFeeApprove($data['suspect_fee_id']);

                $result_array['is_over_standard'] = $data['over_standard_id'] == '' ? 0 : 1;
                $result_array['overStandard'] = $this->getOverStandardApprove($data['over_standard_id']);

                $result_array['is_off_fee'] = $data['official_fee_id'] == '' ? 0 : 1;
                $result_array['official_fee_info'] = $this->getOfficialFeeData($data['official_fee_id']);

                $result_array['is_need_unofficial'] = $data['un_official_app_id'] == '' ? 0 : 1;
                $result_array['unOfficial'] = $this->getUnOfficialFeeApprove($data['un_official_app_id']);

                return $result_array;
            } else {
                return false;
            }
        }

        //辅助函数，获取报销详细信息
        function getDetailTravelFeeItem($feeID){
            $conn = new SQLBase();
            $query = "SELECT * FROM  travel_fee_detail WHERE travel_fee_id = '$feeID'";
            $r = $conn->queryData($query);
            $result_array = array();
            if($r){
                for($i = 0; $i < $r->num_rows; $i++) {
                    $row = $r->fetch_array();
                    $result_array[$i]['start_dest'] =  $row['start_dest'];
                    $result_array[$i]['start_county'] = $row['start_county'];
                    $result_array[$i]['start_time'] = $row['start_time'];
                    $result_array[$i]['end_dest'] = $row['end_dest'];
                    $result_array[$i]['end_county'] = $row['end_county'];
                    $result_array[$i]['end_time'] = $row['end_time'];
                    $result_array[$i]['road_days'] = $row['road_days'];
                    $result_array[$i]['stay_days'] = $row['stay_days'];
                    $result_array[$i]['car_fee'] = $row['car_fee'];
                    $result_array[$i]['boat_fee'] = $row['boat_fee'];
                    $result_array[$i]['flight_fee'] = $row['flight_fee'];
                    $result_array[$i]['hotel_fee'] = $row['hotel_fee'];
                    $result_array[$i]['food_fee'] = $row['food_fee'];
                    $result_array[$i]['inci_fee'] = $row['inci_fee'];
                    $result_array[$i]['other_fee'] = $row['other_fee'];
                    $result_array[$i]['total_fee'] = $row['total_fee'];
                    $result_array[$i]['suspect_fee'] = $row['suspect_fee'];
                    $result_array[$i]['reward_fee'] = $row['reward_fee'];
                    $result_array[$i]['car_main_fee'] = $row['car_main_fee'];
                    $result_array[$i]['oil_fee'] = $row['oil_fee'];
                    $result_array[$i]['pass_fee'] = $row['pass_fee'];
                    $result_array[$i]['distance'] = $row['distance'];
                }
                return $result_array;
            }
            else{
                return false;
            }

        }

        //辅助函数，获取超标审批信息
        function getOverStandardApprove($overAppID)
        {
            $conn = new SQLBase();
            $query = "SELECT over_standard.*, allsection.section_name FROM over_standard, allsection WHERE over_standard_id = '$overAppID' AND over_standard.section_ID = allsection.section_ID";
            $r = $conn->queryData($query);
            $over_fee_data = array();
            if ($r) {
                if ($r->num_rows > 0) {
                    $row = $r->fetch_array();
                    $over_fee_data['over_reason'] = $row['over_reason'];
                    $over_fee_data['section_name'] = $row['section_name'];
                    $over_fee_data['over_standard_id'] = $row['over_standard_id'];
                    $over_fee_data['section_manager_ID'] = $row['section_manager_ID'];
                    $over_fee_data['section_leader_ID'] = $row['section_leader_ID'];
                    $over_fee_data['leader_ID'] = $row['leader_ID'];
                    $over_fee_data['state'] = $row['state'];
                    $over_fee_data['state_detail'] = $row['state_detail'];
                    $over_fee_data['section_approve_opinion'] = $row['section_approve_opinion'];
                    $over_fee_data['section_leader_opinion'] = $row['section_leader_opinion'];
                    $over_fee_data['leader_opinion'] = $row['leader_opinion'];
                    return $over_fee_data;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        //辅助函数，获取嫌疑人审批信息
        function getSuspectFeeApprove($suspectAppID)
        {
            $conn = new SQLBase();
            $query = "SELECT suspect_money.*, allsection.section_name FROM suspect_money, allsection 
                      WHERE suspect_ID = '$suspectAppID' AND suspect_money.section_ID = allsection.section_ID";
            $r = $conn->queryData($query);
            $suspect_fee_data = array();
            if ($r) {
                if ($r->num_rows > 0) {
                    $row = $r->fetch_array();
                    $suspect_fee_data['section_name'] = $row['section_name'];
                    $suspect_fee_data['suspect_ID'] = $row['suspect_ID'];
                    $suspect_fee_data['suspect_name'] = $row['suspect_name'];
                    $suspect_fee_data['suspect_road_fee'] = $row['suspect_travel_money'];
                    $suspect_fee_data['suspect_life_fee'] = $row['suspect_life_money'];
                    $suspect_fee_data['suspect_other_fee'] = $row['suspect_other_money'];
                    $suspect_fee_data['has_receipe'] = $row['has_receipe'];
                    $suspect_fee_data['no_receipe_reason'] = $row['no_receipe_reason'];
                    $suspect_fee_data['section_manager_ID'] = $row['section_manager_ID'];
                    $suspect_fee_data['section_leader_ID'] = $row['section_leader_ID'];
                    $suspect_fee_data['leader_ID'] = $row['leader_ID'];
                    $suspect_fee_data['state'] = $row['state'];
                    $suspect_fee_data['state_detail'] = $row['state_detail'];
                    $suspect_fee_data['section_approve_opinion'] = $row['section_approve_opinion'];
                    $suspect_fee_data['section_leader_opinion'] = $row['section_leader_opinion'];
                    $suspect_fee_data['leader_opinion'] = $row['leader_opinion'];
                    return $suspect_fee_data;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        //辅助函数，获取未使用公务卡报销强制费用审批信息
        function getUnOfficialFeeApprove($unoffAppID){
            $conn = new SQLBase();
            $query = "SELECT unuse_official_approve.*, allsection.section_name FROM unuse_official_approve, allsection 
                      WHERE unuse_official_id = '$unoffAppID' AND unuse_official_approve.section_ID = allsection.section_ID";
            $r = $conn->queryData($query);

            $un_fee_data = array();
            if ($r) {
                if ($r->num_rows > 0) {
                    $row = $r->fetch_array();
                    $un_fee_data['section_name'] = $row['section_name'];
                    $un_fee_data['unuse_official_id'] = $row['unuse_official_id'];
                    $un_fee_data['reason'] = $row['reason'];
                    $un_fee_data['section_manager_ID'] = $row['section_manager_ID'];
                    $un_fee_data['section_leader_ID'] = $row['section_leader_ID'];
                    $un_fee_data['leader_ID'] = $row['leader_ID'];
                    $un_fee_data['state'] = $row['state'];
                    $un_fee_data['state_detail'] = $row['state_detail'];
                    $un_fee_data['section_approve_opinion'] = $row['section_approve_opinion'];
                    $un_fee_data['section_leader_opinion'] = $row['section_leader_opinion'];
                    $un_fee_data['leader_opinion'] = $row['leader_opinion'];
                    return $un_fee_data;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        //辅助函数，获取公务卡报销信息
        function getOfficialFeeData($feeID){
            $conn = new SQLBase();
            $sql = "SELECT * FROM official_fee_item WHERE official_fee_id = '$feeID'";
            $r = $conn->queryData($sql);

            if($r){
                $row = $r->fetch_array();
                $result_array = array(
                    'official_hotel_fee'=>$row['official_hotel_fee'],
                    'official_oil_fee'=>$row['official_oil_fee'],
                    'official_plane_fee'=>$row['official_plane_fee'],
                    'official_car_maintain_fee'=>$row['official_car_maintain_fee'],
                    'official_fee'=>$row['official_fee'],
                    'max_official_hotel_fee'=>$row['need_official_hotel_fee'],
                    'max_official_oil_fee'=>$row['need_official_oil_fee'],
                    'max_official_car_maintain_fee'=>$row['need_official_car_maintain_fee'],
                    'max_official_plane_fee'=>$row['need_official_plane_fee'],
                    'max_official_fee'=>$row['need_official_fee']
                    );
                return $result_array;
            }
            else{
                return false;
            }
        }

        //辅助函数，按部门差旅报销费用信息,部门ID或部门ID数组，状态
        function getTravelFeeApproveBySection($sectionID, $allsection, $state){
            if($allsection == 0){
                $sql = "SELECT travel_approval.*, b.*, allsection.section_name, user.user_name FROM travel_approval INNER JOIN allsection, user, travel_fee_approve as b
                  WHERE travel_approval.section_ID = '$sectionID' AND travel_approval.section_ID = allsection.section_ID 
                  AND travel_approval.write_ID = user.user_ID AND b.state_detail ='$state' AND b.state = '0' 
                  AND b.travel_approve_id = travel_approval.expense_claim_ID AND b.is_dispose = 0";
            }
            else{
                $sql = "SELECT travel_approval.*, b.*, allsection.section_name, user.user_name FROM travel_approval INNER JOIN allsection, user, travel_fee_approve as b
                  WHERE travel_approval.section_ID = allsection.section_ID AND travel_approval.write_ID = user.user_ID AND b.is_dispose = 0 
                  AND b.state_detail ='$state' AND b.travel_approve_id = travel_approval.expense_claim_ID AND b.state = '0' AND travel_approval.section_ID IN (";
                for($i = 0; $i < count($sectionID); $i++){
                    $sql .= "'$sectionID[$i]'";
                    if($i != count($sectionID)-1){
                        $sql .= ",";
                    }
                    else{
                        $sql .= ")";
                    }
                }
            }
            $conn = new SQLBase();
            $r = $conn->queryData($sql);
            if($r){
                $result_array['state'] = 1;
                $result_array['state_error'] = '';
                $result_array['data'] = array();
                for($i = 0; $i < $r->num_rows; $i++){
                    $row = $r->fetch_array();
                    $result_array['data'][$i] = array('type'=>1, 'user_name'=>$row['user_name'], 'user_id'=>$row['write_ID'], 'section_id'=>$row['section_ID'],
                        'travel_type'=>$row['travel_type'], 'app_time'=>$row['write_time'], 'app_id'=>$row['expense_claim_ID'], 'section_name'=>$row['section_name'], 'fee_id'=>$row['travel_fee_id']);
                }
                $result_array['total_count'] = $r->num_rows;
            }
            else{
                $result_array['state'] = 0;
                $result_array['state_error'] = 'DB_ERROR';
            }
            return $result_array;

        }

        //辅助函数，财务审核时，获取相关信息
        function getFinanceFeeApprove($sectionID, $username){
            $sql = "SELECT travel_approval.*, travel_fee_approve.*, allsection.section_name, user.user_name, official_fee_item.official_fee
                  FROM travel_approval INNER JOIN allsection, user, travel_fee_approve, official_fee_item 
                  WHERE travel_approval.section_ID = allsection.section_ID AND travel_approval.write_ID = user.user_ID 
                  AND travel_fee_approve.state ='1' AND travel_fee_approve.travel_approve_id = travel_approval.expense_claim_ID
                  AND travel_fee_approve.official_fee_id = official_fee_item.official_fee_id";
            if($sectionID != 0){
                $sql .= " AND travel_approval.section_ID = '$sectionID'";
            }
            if($username != ''){
                $sql .= " AND user.user_name LIKE '$username%'";
            }

            $conn = new SQLBase();
            $result_array = array();
            $r = $conn->queryData($sql);
            if($r){
                $result_array['state'] = 1;
                $result_array['state_error'] = '';
                $result_array['data'] = array();
                for($i = 0; $i < $r->num_rows; $i++){
                    $row = $r->fetch_array();
                    $result_array['data'][$i] = array('type'=>1, 'user_name'=>$row['user_name'],
                        'section_name'=>$row['section_name'], 'app_time'=>$row['write_time'],
                        'travel_type'=>$row['travel_type'], 'app_id'=>$row['expense_claim_ID'],
                        'pre_total_fee'=>$row['pre_total_fee'], 'pre_official_fee'=>$row['official_fee'], 'fee_id'=>$row['travel_fee_id']);

                }
                $result_array['total_count'] = $r->num_rows;
            }
            else{
                $result_array['state'] = 0;
                $result_array['state_error'] = 'DB_ERROR';
            }
            return $result_array;
        }

        //辅助函数，审批整个信息
        //1 审批基本费用报销信息
        //2 审批嫌疑人费用信息，如果有
        //3 审批超标费用信息，如果有
        //4 审批公务卡费用信息，如果有
        function approveBusinessFeeState($data){
            $conn = new SQLBase();
            $conn->queryData('SET AUTOCOMMIT=0');
            $conn->queryData("BEGIN");
            $return_array = array();
            if(!$this->approveBasicFeeState($data)){
                $conn->queryData('ROLLBACK');
                return array('state'=>0, 'error'=>'APPROVE_BASIC_FAILED');
            }

            if($data['approve_suspect'] == 1){
                $r = $this->approveSuspectFee($data);
                if(!$r){
                    $conn->queryData('ROLLBACK');
                    return array('state'=>0, 'error'=>'APPROVE_SUSPECT_FAILED');
                }
            }

            if($data['approve_overstandard'] == 1){
                $r = $this->approveOverstandardFee($data);
                if(!$r){
                    $conn->queryData('ROLLBACK');
                    return array('state'=>0, 'error'=>'APPROVE_OVER_FAILED');
                }
            }

            if($data['approve_unofficial'] == 1){
                $r = $this->approveUnofficialFee($data);
                if(!$r){
                    $conn->queryData('ROLLBACK');
                    return array('state'=>0, 'error'=>'APPROVE_OVER_FAILED');
                }
            }

            $conn->queryData('COMMIT');
            $conn->queryData('SET AUTOCOMMIT=1');

            return array('state'=>1, 'error'=>'');

        }

        //审批基本费用报销信息
        function approveBasicFeeState($data)
        {
            $sql = '';
            $userID = $data['userID'];
            $opinion = $data['basic_fee_opinion']['basic_fee_opinion'];
            $opinion_detail = $data['basic_fee_opinion']['basic_fee_opinion_detail'];
            $feeID = $data['approve_id'];
            $type = $data['approve_state'];
            if ($opinion == 1) {
                switch ($type) {
                    case 1:
                        $sql = "UPDATE travel_fee_approve SET section_manager_ID='$userID', state_detail='02', section_approve_opinion='$opinion_detail' WHERE travel_fee_id='$feeID'";
                        break;
                    case 2:
                        $sql = "UPDATE travel_fee_approve SET section_leader_ID='$userID', state_detail='03', section_leader_opinion='$opinion_detail' WHERE travel_fee_id='$feeID'";
                        break;
                    case 3:
                        $sql = "UPDATE travel_fee_approve SET leader_ID='$userID', state='1', state_detail='11', leader_opinion='$opinion_detail' WHERE travel_fee_id='$feeID'";
                        break;
                }
            } else {
                switch ($type) {
                    case 1:
                        $sql = "UPDATE travel_fee_approve SET section_manager_ID='$userID', state='2', state_detail='21', section_approve_opinion='$opinion_detail' WHERE travel_fee_id='$feeID'";
                        break;
                    case 2:
                        $sql = "UPDATE travel_fee_approve SET section_leader_ID='$userID', state='2', state_detail='22', section_leader_opinion='$opinion_detail' WHERE travel_fee_id='$feeID'";
                        break;
                    case 3:
                        $sql = "UPDATE travel_fee_approve SET leader_ID='$userID', state='2', state_detail='23', leader_opinion='$opinion_detail' WHERE travel_fee_id='$feeID'";
                        break;
                }

            }

            $conn = new SQLBase();

            $r = $conn->queryData($sql);
            if ($r) {
                return true;
            } else {
                return false;
            }
        }

        //辅助函数，审批嫌疑人费用
        function approveSuspectFee($data)
        {
            $conn = new SQLBase();
            $sql = '';
            $userID = $data['userID'];
            $opinion = $data['suspect_opinion']['suspect_opinion'];
            $opinion_detail = $data['suspect_opinion']['suspect_opinion_detail'];
            $suspect_id = $data['suspect_id'];
            $type = $data['approve_state'];
            if ($opinion == 1) {
                switch ($type) {
                    case 1:
                        $sql = "UPDATE suspect_money SET section_manager_ID='$userID', state_detail='02', section_approve_opinion='$opinion_detail' WHERE suspect_ID='$suspect_id'";
                        break;
                    case 2:
                        $sql = "UPDATE suspect_money SET section_leader_ID='$userID', state_detail='03', section_leader_opinion='$opinion_detail' WHERE suspect_ID='$suspect_id'";
                        break;
                    case 3:
                        $sql = "UPDATE suspect_money SET leader_ID='$userID', state='1', state_detail='11', leader_opinion='$opinion_detail' WHERE suspect_ID='$suspect_id'";
                        break;
                }
            } else {
                switch ($type) {
                    case 1:
                        $sql = "UPDATE suspect_money SET section_manager_ID='$userID', state='2', state_detail='21', section_approve_opinion='$opinion_detail' WHERE suspect_ID='$suspect_id'";
                        break;
                    case 2:
                        $sql = "UPDATE suspect_money SET section_leader_ID='$userID', state='2', state_detail='22', section_leader_opinion='$opinion_detail' WHERE suspect_ID='$suspect_id'";
                        break;
                    case 3:
                        $sql = "UPDATE suspect_money SET leader_ID='$userID', state='2', state_detail='23', leader_opinion='$opinion_detail' WHERE suspect_ID='$suspect_id'";
                        break;
                }
            }
            $r = $conn->queryData($sql);
            if($r){
                return true;
            }
            else{
                return false;
            }

        }

        //辅助函数，审批超标交通费用
        function approveOverstandardFee($data){
            $conn = new SQLBase();
            $sql = '';
            $userID = $data['userID'];
            $opinion = $data['overstandard_opinion']['over_opinion'];
            $opinion_detail = $data['overstandard_opinion']['over_opinion_detail'];
            $overID = $data['over_standard_id'];
            $type = $data['approve_state'];
            if ($opinion == 1) {
                switch ($type) {
                    case 1:
                        $sql = "UPDATE over_standard SET section_manager_ID='$userID', state_detail='02', section_approve_opinion='$opinion_detail' WHERE over_standard_id='$overID'";
                        break;
                    case 2:
                        $sql = "UPDATE over_standard SET section_leader_ID='$userID', state_detail='03', section_leader_opinion='$opinion_detail' WHERE over_standard_id='$overID'";
                        break;
                    case 3:
                        $sql = "UPDATE over_standard SET leader_ID='$userID', state='1', state_detail='11', leader_opinion='$opinion_detail' WHERE over_standard_id='$overID'";
                        break;
                }
            } else {
                switch ($type) {
                    case 1:
                        $sql = "UPDATE over_standard SET section_manager_ID='$userID', state='2', state_detail='21', section_approve_opinion='$opinion_detail' WHERE over_standard_id='$overID'";
                        break;
                    case 2:
                        $sql = "UPDATE over_standard SET section_leader_ID='$userID', state='2', state_detail='22', section_leader_opinion='$opinion_detail' WHERE over_standard_id='$overID'";
                        break;
                    case 3:
                        $sql = "UPDATE over_standard SET leader_ID='$userID', state='2', state_detail='23', leader_opinion='$opinion_detail' WHERE over_standard_id='$overID'";
                        break;
                }
            }
            $r = $conn->queryData($sql);
            if($r){
                return true;
            }
            else{
                return false;
            }
        }

        //辅助函数，审批超标交通费用
        function approveUnofficialFee($data){
            $conn = new SQLBase();
            $sql = '';
            $userID = $data['userID'];
            $opinion = $data['unofficial_opinion']['unofficial_opinion'];
            $opinion_detail = $data['unofficial_opinion']['unofficial_opinion_detail'];
            $unuseID = $data['unuse_official_id'];
            $type = $data['approve_state'];
            if ($opinion == 1) {
                switch ($type) {
                    case 1:
                        $sql = "UPDATE unuse_official_approve SET section_manager_ID='$userID', state_detail='02', section_approve_opinion='$opinion_detail' WHERE unuse_official_id='$unuseID'";
                        break;
                    case 2:
                        $sql = "UPDATE unuse_official_approve SET section_leader_ID='$userID', state_detail='03', section_leader_opinion='$opinion_detail' WHERE unuse_official_id='$unuseID'";
                        break;
                    case 3:
                        $sql = "UPDATE unuse_official_approve SET leader_ID='$userID', state='1', state_detail='11', leader_opinion='$opinion_detail' WHERE unuse_official_id='$unuseID'";
                        break;
                }
            } else {
                switch ($type) {
                    case 1:
                        $sql = "UPDATE unuse_official_approve SET section_manager_ID='$userID', state='2', state_detail='21', section_approve_opinion='$opinion_detail' WHERE unuse_official_id='$unuseID'";
                        break;
                    case 2:
                        $sql = "UPDATE unuse_official_approve SET section_leader_ID='$userID', state='2', state_detail='22', section_leader_opinion='$opinion_detail' WHERE unuse_official_id='$unuseID'";
                        break;
                    case 3:
                        $sql = "UPDATE unuse_official_approve SET leader_ID='$userID', state='2', state_detail='23', leader_opinion='$opinion_detail' WHERE unuse_official_id='$unuseID'";
                        break;
                }
            }
            $r = $conn->queryData($sql);
            if($r){
                return true;
            }
            else{
                return false;
            }
        }

        //辅助函数，财务审批信息
        //1 添加finance_approve_business_fee
        //2 添加finance_approve_business_fee_item
        //3 处理相关借款
        //4 处理相关审批单状态
        function financeCheckBusinessFee($data){
            $conn = new SQLBase();
            $conn->queryData('SET AUTOCOMMIT = 0');
            $conn->queryData('BEGIN');
            //1 添加finance_approve_business_fee
            //2 添加finance_approve_business_fee_item
            $r = $this->createFinanceApproveBusinessFee($data);
            if(!$r){
                $conn->queryData('ROLLBACK');
                $conn->queryData('SET AUTOCOMMIT = 1');
                exit(json_encode(array('state'=>0, 'error'=>'INSERT_FINANCE_ERROR')));
            }

            //3 处理相关借款
            $loan_fee_id = $data['loan_fee_id'];
            $check_loan_fee = $data['loan_fee'];
            $user_id = $data['user_id'];
            $l_fee = new LoanFee();
            $r = $l_fee->financeCheckLoanFee($loan_fee_id, $check_loan_fee, $user_id);

            //4 处理相关审批单状态
            //4.1 更新travel_fee_approve为已报销
            $fee_id = $data['travel_fee_id'];
            $sql = "UPDATE travel_fee_approve SET state = 3 WHERE travel_fee_id = '$fee_id'";
            $r = $conn->queryData($sql);
            //4.2 更新travel_approval为已报销
            $app_id = $data['travel_app_id'];
            $sql = "UPDATE travel_approval SET state = 4 WHERE expense_claim_ID = '$app_id'";
            $r = $conn->queryData($sql);
            //4.3 更新unuse_official_approve为已报销
            $sql = "UPDATE unuse_official_approve INNER JOIN travel_fee_approve SET unuse_official_approve.state = 3
                        WHERE unuse_official_approve.unuse_official_id = travel_fee_approve.un_official_app_id
                        AND travel_fee_approve.travel_fee_id = '$fee_id'";
            $r = $conn->queryData($sql);
            //4.4 更新over_standard为已报销
            $sql = "UPDATE over_standard INNER JOIN travel_fee_approve SET over_standard.state = 3
                        WHERE over_standard.over_standard_id = travel_fee_approve.over_standard_id
                        AND travel_fee_approve.travel_fee_id = '$fee_id'";
            $r = $conn->queryData($sql);
            //4.5 更新suspect_fee为已报销
            $sql = "UPDATE suspect_money INNER JOIN travel_fee_approve SET suspect_money.state = 3
                        WHERE suspect_money.suspect_ID = travel_fee_approve.suspect_fee_id
                        AND travel_fee_approve.travel_fee_id = '$fee_id'";
            $r = $conn->queryData($sql);

            if(!$r){
                $conn->queryData('ROLLBACK');
                $conn->queryData('SET AUTOCOMMIT = 1');
                exit(json_encode(array('state'=>0, 'error'=>'INSERT_FINANCE_ERROR')));
            }
            else{
                $conn->queryData('COMMIT');
                $conn->queryData('SET AUTOCOMMIT = 1');
                echo json_encode(array('state'=>1, 'error'=>''));
            }

        }

        private function createFinanceApproveBusinessFee($data){
            $id = uniqid();
            $conn = new SQLBase();
            $sql = "INSERT INTO finance_approve_business_fee VALUES('$id','".$data['travel_fee_id']."','".$data['section_id']."','"
                .$data['user_id']."','".$data['total_fuhe_fee']."','".$data['official_fee']."','".$data['section_fee']."',NOW())";
            $r = $conn->queryData($sql);
            if($r){
                return $this->createFinanceApproveBusinessFeeItem($data, $id);
            }
            else{
                return false;
            }
        }

        private function createFinanceApproveBusinessFeeItem($feeData, $feeID){
            $conn = new SQLBase();

            for ($i = 0; $i < count($feeData['data']); $i++) {
                $query = "INSERT finance_approve_business_fee_detail(finance_fee_id, start_dest, start_county, start_time, end_dest, 
                        end_county, end_time, stay_days, road_days, car_fee, boat_fee, flight_fee, hotel_fee, food_fee, 
                        inci_fee, other_fee, suspect_fee, reward_fee, car_main_fee,
                        oil_fee, pass_fee, distance) VALUES
                        ('" . $feeID . "'";
                $query .= ",'" . $feeData['data'][$i]['start_addr'] . "','" . $feeData['data'][$i]['start_county'] . "','"
                    . $feeData['data'][$i]['start_time'] . "','" . $feeData['data'][$i]['end_addr'] . "','"
                    . $feeData['data'][$i]['end_county'] . "','" . $feeData['data'][$i]['end_time'] . "','"
                    . $feeData['data'][$i]['stay_days'] . "','" . $feeData['data'][$i]['road_days'] . "','"
                    . $feeData['data'][$i]['car_fee'] . "','" . $feeData['data'][$i]['boat_fee'] . "','"
                    . $feeData['data'][$i]['flight_fee'] . "','" . $feeData['data'][$i]['hotel_fee'] . "','"
                    . $feeData['data'][$i]['food_fee'] . "','" . $feeData['data'][$i]['inci_fee'] . "','"
                    . $feeData['data'][$i]['other_fee'] . "','" . $feeData['data'][$i]['suspect_fee'] . "','"
                    . $feeData['data'][$i]['reward_fee'] ."','" . $feeData['data'][$i]['car_main_fee'] . "','"
                    . $feeData['data'][$i]['oil_fee'] ."','" . $feeData['data'][$i]['pass_fee'] . "','"
                    . $feeData['data'][$i]['distance']."')";

                $r = $conn->queryData($query);
                if (!$r) {
                    return false;
                }
            }
            return true;
        }

        //辅助函数，获取用于创建PDF的数据
        function getBusinessFeePDFData($feeID){
            $data = $this->getBusinessFeeDetail($feeID);
            if(!$data){
                return false;
            }
            if($data['fee_data']['state'] == 3){
                $isCheckd = true;
            }
            else{
                $isCheckd = false;
            }
            $pdfData = array(
                'fee_id'=>$data['fee_data']['travel_fee_id'],
                'name'=>$data['travel_app_data']['approval_user_name'],
                'department_name'=>$data['travel_app_data']['section_name'],
                'case_name'=>$data['travel_app_data']['case_name'],
                'bankcard'=>$data['travel_app_data']['approval_user_card'],
                'loan_money'=>$data['travel_app_data']['is_loan_money'] == 0 ? 0 : $data['travel_app_data']['loan_money']['money'],
                'section_manager_ID'=>$data['fee_data']['section_manager_ID'],
                'section_leader_ID'=>$data['fee_data']['section_leader_ID'],
                'leader_ID'=>$data['fee_data']['leader_ID'],
                'section_approve_opinion'=>$data['fee_data']['section_approve_opinion'],
                'section_leader_opinion'=>$data['fee_data']['section_leader_opinion'],
                'leader_opinion'=>$data['fee_data']['leader_opinion'],
                'self_drive'=>$data['travel_app_data']['self_drive'],
                'business_type'=>$this->getBusinessType($data['travel_app_data']['type'])
                );

            $pdfData['apply_detail'] = array();
            if($isCheckd){
                $feeData = $data['finance_check_item_data'];
            }
            else{
                $feeData = $data['fee_data_item'];
            }

            for($i = 0; $i < count($feeData); $i++){
                $pdfData['apply_detail'][$i] =array(
                    'depart_time'=>date('y/m/d', strtotime($feeData[$i]['start_time'])),
                    'depart_city'=>explode('/', $feeData[$i]['start_dest'])[1],
                    'arrive_time'=>date('y/m/d', strtotime($feeData[$i]['end_time'])),
                    'arrive_city'=>explode('/', $feeData[$i]['end_dest'])[1],
                    'car_fee'=>$feeData[$i]['car_fee'],
                    'boat_fee'=>$feeData[$i]['boat_fee'],
                    'flight_fee'=>$feeData[$i]['flight_fee'],
                    'road_days'=>$feeData[$i]['road_days'],
                    'stay_days'=>$feeData[$i]['stay_days'],
                    'total_days'=>$feeData[$i]['road_days'] + $feeData[$i]['stay_days'],
                    'hotel_fee'=>$feeData[$i]['hotel_fee'],
                    'food_fee'=>$feeData[$i]['food_fee'],
                    'inci_fee'=>$feeData[$i]['inci_fee'],
                    'other_fee'=>$feeData[$i]['other_fee'],
                    'suspect_fee'=>$feeData[$i]['suspect_fee'],
                    'reward_fee'=>$feeData[$i]['reward_fee'],
                    'car_main_fee'=>$feeData[$i]['car_main_fee'],
                    'oil_fee'=>$feeData[$i]['oil_fee'],
                    'pass_fee'=>$feeData[$i]['pass_fee'],
                    'distance'=>$feeData[$i]['distance']);
            }
            $pdfData['official_fee'] = $isCheckd ? $data['finance_check_data']['official_fee']
                : $data['fee_data']['official_fee_info']['official_fee'];
            $pdfData['total_fee'] = $isCheckd ? $data['finance_check_data']['total_fee']
                : $data['fee_data']['pre_total_fee'];

            return $pdfData;
        }

        function getBusinessType($type){
            switch($type){
                case 1:
                    return '业务出差';
                case 2:
                    return '大案要案';
                case 3:
                    return '会议';
                case 4:
                    return '培训';
            }

        }

        //辅助函数，驳回财务
        function financeDiffuseBusinessFee($fee_id){
            $conn = new SQLBase();
            $query = "UPDATE travel_fee_approve SET state = '2', state_detail = '24' WHERE travel_fee_id = '$fee_id'";
            $r = $conn->queryData($query);
            if($r){
                return true;
            }
            else{
                return false;
            }
        }

        //辅助函数，获取未使用公务卡报销数据
        function getUnOfficialAppPDFData($fee_id){
            $conn = new SQLBase();
            $query = "SELECT unuse_official_approve.*, official_fee_item.*, allsection.section_name, user.user_name 
                    FROM travel_fee_approve, unuse_official_approve, official_fee_item, allsection, user
                    WHERE travel_fee_approve.travel_fee_id = '$fee_id' AND travel_fee_approve.official_fee_id = official_fee_item.official_fee_id
                    AND unuse_official_approve.unuse_official_id = travel_fee_approve.un_official_app_id 
                    AND user.user_ID = unuse_official_approve.write_ID AND allsection.section_ID = unuse_official_approve.section_ID";
           // echo $query;
            $r = $conn->queryData($query);
            if($r){
                $row = $r->fetch_array();
                $result_array = array('fee_id'=>$fee_id, 'unuse_official_id'=>$row['unuse_official_id'],
                    'user_name'=>$row['user_name'], 'section_name'=>$row['section_name'],
                    'reason'=>$row['reason'], 'section_manager_ID'=>$row['section_manager_ID'],
                    'section_leader_ID'=>$row['section_leader_ID'], 'leader_ID'=>$row['leader_ID'],
                    'write_time'=>$row['write_time'], 'section_approve_opinion'=>$row['section_approve_opinion'],
                    'section_leader_opinion'=>$row['section_leader_opinion'], 'leader_opinion'=>$row['leader_opinion']);
                $result_array['fee_data'] = array('official_hotel_fee'=>$row['official_hotel_fee'],
                    'official_plane_fee'=>$row['official_plane_fee'], 'official_oil_fee'=>$row['official_oil_fee'],
                    'official_car_maintain_fee'=>$row['official_car_maintain_fee'], 'official_fee'=>$row['official_fee'],
                    'need_official_hotel_fee'=>$row['need_official_hotel_fee'], 'need_official_plane_fee'=>$row['need_official_plane_fee'],
                    'need_official_oil_fee'=>$row['need_official_oil_fee'],'need_official_car_maintain_fee'=>$row['need_official_car_maintain_fee'],
                    'need_official_fee'=>$row['need_official_fee']);
                return $result_array;
            }
            return false;
        }





    }


?>